"""
-*- coding: utf-8 -*-
@Author  : Link
@Time    : 2023/1/13 10:46
@Site    : 
@File    : site_variable.py
@Software: PyCharm
@Remark  : 
"""
from dataclasses import dataclass
from typing import List, Dict

from BenchCore.stdf_interface.stdf_def_interface import Prr, Ptr, Pir


@dataclass
class CacheBin:
    HARD_BIN: int = 1
    HARD_BIN_NM: str = ""
    SOFT_BIN: int = 1
    SOFT_BIN_NM: str = ""
    BIN_P_F: str = "P"


class SiteVariable:
    """
    TODO: Site内的信号使用这个发送
    """
    dut = 0
    prr: Prr = None
    ptr_list: List[Ptr] = None
    dtr_list: List[str] = None
    pir: Pir = None
    func_pass = True  # TODO: 重要, 单个测试项目是否PASS
    bin_block = False

    hbr: Dict[int, list] = None
    sbr: Dict[int, list] = None

    # local
    x_coord = 0
    y_coord = 0

    def __init__(self, dut: int = 0):
        super(SiteVariable).__init__()
        self.dut = dut
        self.init()
        self.cache = CacheBin()

    def init(self):
        """
        固定
        :return:
        """
        self.func_pass = True
        self.ptr_list = list()
        self.dtr_list = list()
        self.hbr = dict()
        self.sbr = dict()
        self.gen_pir()

    def gen_pir(self):
        self.pir = Pir(HEAD_NUM=1, SITE_NUM=self.dut)

    def variable_update(self, **kwargs):
        """
        单颗测试前调用, 清除内存缓存的数据
        :param kwargs:
        :return:
        """
        self.prr = Prr(
            HEAD_NUM=1,
            SITE_NUM=self.dut,
            **kwargs
        )
        self.ptr_list.clear()
        self.dtr_list.clear()

    def post_uut_test(self, app_var=None):
        if self.cache.HARD_BIN not in self.hbr:
            self.hbr[self.cache.HARD_BIN] = [self.cache.HARD_BIN, self.cache.HARD_BIN_NM, self.cache.BIN_P_F, 1]
        else:
            self.hbr[self.cache.HARD_BIN][3] += 1
        if self.cache.SOFT_BIN not in self.sbr:
            self.sbr[self.cache.SOFT_BIN] = [self.cache.SOFT_BIN, self.cache.SOFT_BIN_NM, self.cache.BIN_P_F, 1]
        else:
            self.sbr[self.cache.SOFT_BIN][3] += 1
        if app_var is None:
            return
        if self.cache.HARD_BIN not in app_var.hbr:
            app_var.hbr[self.cache.HARD_BIN] = [self.cache.HARD_BIN, self.cache.HARD_BIN_NM, self.cache.BIN_P_F, 1]
        else:
            app_var.hbr[self.cache.HARD_BIN][3] += 1
        if self.cache.SOFT_BIN not in app_var.sbr:
            app_var.sbr[self.cache.SOFT_BIN] = [self.cache.SOFT_BIN, self.cache.SOFT_BIN_NM, self.cache.BIN_P_F, 1]
        else:
            app_var.sbr[self.cache.SOFT_BIN][3] += 1
